<?php
/**
 * Load styles and scripts
 * 
 */
function larch_scripts(){
	wp_enqueue_style('style',  get_stylesheet_uri());
}
add_action( 'wp_enqueue_scripts', 'larch_scripts' );

/**
 * Setting default content width
 * 
 */
if(!isset($content_width)){
	$content_width = 474;
}

if(!function_exists( 'larch_setup')){
	function larch_setup(){
		//load text domain
		load_theme_textdomain('larch',  get_template_directory().'/lang');
		add_theme_support('post-thumbnails');
		set_post_thumbnail_size(672,372,true);
		add_image_size('larch-full-width', 1038, 576, true);
		//register navigations
		register_nav_menus(array(
			'primary' => __('Main menu','larch'),
			'secondary' => __('Sidebar menu','larch'),
			'ternary' => ('Other navigation on a single page'),
		));
		add_theme_support('html5',array(
			'search-form','comment-form','comment-list'
		));
		add_theme_support('post-formats',array(
			'image','video','link','gallery'
		));
		add_theme_support('custom-background',array('default-color'=>'f5f5f5'));
		
	}
}
add_action('after_setup_theme','larch_setup');
if(!function_exists( 'register_navs')){
	function register_navs(){
		register_sidebar(array(
			'name'=>__(''),
		));
	}
}
add_action('widgets_init','register_navs');

/**
 * Theme options
 * 
 */
add_action('admin_menu','larch_admin_menu');
if(!function_exists( 'larch_admin_menu')){
	function larch_admin_menu(){
		add_menu_page('Larch Theme Options', 'Site Theme Options', 'administrator', 'larch_admin_options_page', 'create_larch_admin');
	}
}
function create_larch_admin(){ ?>
<div class="wrap">
	<h2>Website Settings</h2>
	<small>change this website's settings</small>
	<?php	 settings_errors(); ?>
	<section>
		<form method="post" action ="options.php">
			<?php
			 settings_fields( 'larch_admin_settings');
			 do_settings_sections( 'larch_admin_settings');
			 submit_button();
			?>
		</form>
	</section>
</div>
<?php }
	function init_larch_admin_options(){
		if(false==get_option('larch_admin_settings')){
						add_option('larch_admin_settings');}
		register_setting('larch_admin_settings','larch_admin_settings');
		add_settings_section('larch_admin_header', 'Header Settings', 'display_section', 'larch_admin_settings');
		 $field_args = array(
			'type'      => 'checkbox',
			'id'        => 'larch_show_icons',
			'name'      => 'larch_show_icons',
			'desc'      => 'Check to display social icons',
			'std'       => '',
			'label_for' => 'larch_show_icons',
			'class'     => 'css_class'
		  );
		  add_settings_field( $field_args['id'], 'Display Social Icons', 'larch_display_input', 'larch_admin_settings', 'larch_admin_header', $field_args );
		  $field_args = array(
			'type'      => 'text',
			'id'        => 'larch_fb_url',
			'name'      => 'larch_fb_url',
			'desc'      => 'starts with https:',
			'std'       => '',
			'label_for' => 'larch_fb_url',
			'class'     => 'css_class'
		  );
		  add_settings_field( $field_args['id'], 'Facebook URL', 'larch_display_input', 'larch_admin_settings', 'larch_admin_header', $field_args );
	}
add_action('admin_init','init_larch_admin_options');
function display_section(){
	echo '<small>Change header settings here</small>';
}
function larch_display_input($cb_args){
	extract($cb_args);
	$option_group = 'larch_admin_settings';
	$option = get_option($option_group);
	$option[$id] = stripslashes($option[$id]);
	$option[$id] = esc_attr($option[$id]);
	switch ( $type) {
		case 'text':
			echo "<input class= 'regular-text $class' type='$type' id ='$id' name='$option_group[$id]' value='$option[$id]'/>";
			echo ($desc != '') ? "<label for = '$id'>$desc</label>" : "";
			break;
		case 'checkbox':
			echo "<input class= 'regular-text$class' type='$type' id ='$id' name='".$option_group."[$id]'  value='1' ".checked(1,$option[$id],false)."/>";
			echo ($desc != '') ? "<label for = '$id'>$desc</label>" : "";
			break;
		default:
			break;
	}
	
}
